package org.tailor

import org.codehaus.groovy.grails.web.context.ServletContextHolder
import org.springframework.web.multipart.commons.CommonsMultipartFile
import equitiestailor.FileUploadService
class TailorController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    public directory = 'tailor'  
    def FileUploadService fileUploadService
    
    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [tailorInstanceList: Tailor.list(params), tailorInstanceTotal: Tailor.count()]
    }

    def create = {
        def tailorInstance = new Tailor()
        tailorInstance.properties = params
        return [tailorInstance: tailorInstance]
    }

    def save = {
        def tailorInstance = new Tailor(params)
        tailorInstance.url = fileUploadService.createImage(directory,params,request)
        if (tailorInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'tailor.label', default: 'Tailor'), tailorInstance.id])}"
            redirect(action: "show", id: tailorInstance.id)
        }
        else {
            render(view: "create", model: [tailorInstance: tailorInstance])
        }
    }

    def show = {
        def tailorInstance = Tailor.get(params.id)
        if (!tailorInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'tailor.label', default: 'Tailor'), params.id])}"
            redirect(action: "list")
        }
        else {
            [tailorInstance: tailorInstance]
        }
    }

    def edit = {
        def tailorInstance = Tailor.get(params.id)
        if (!tailorInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'tailor.label', default: 'Tailor'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [tailorInstance: tailorInstance]
        }
    }

    def update = {
        def tailorInstance = Tailor.get(params.id)
        if (tailorInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (tailorInstance.version > version) {
                    
                    tailorInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'tailor.label', default: 'Tailor')] as Object[], "Another user has updated this Tailor while you were editing")
                    render(view: "edit", model: [tailorInstance: tailorInstance])
                    return
                }
            }
            tailorInstance.properties = params
            def fileName = fileUploadService.createImage(directory,params,request)
            if (fileName != ""){
                tailorInstance.url = fileName
            }
            if (!tailorInstance.hasErrors() && tailorInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'tailor.label', default: 'Tailor'), tailorInstance.id])}"
                redirect(action: "show", id: tailorInstance.id)
            }
            else {
                render(view: "edit", model: [tailorInstance: tailorInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'tailor.label', default: 'Tailor'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def tailorInstance = Tailor.get(params.id)
        if (tailorInstance) {
            try {
                tailorInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'tailor.label', default: 'Tailor'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'tailor.label', default: 'Tailor'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'tailor.label', default: 'Tailor'), params.id])}"
            redirect(action: "list")
        }
    }
}
